﻿using Approval.API.Applcation.Command.PurchaseRelationCommand;
using Approval.Domain;
using Approval.Domain.Common;
using Approval.Infrastructure.Impl;
using Approval.Infrastructure.Interface;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Approval.API.Applcation.CommandHandler.PurchaseRelationCommandHandler
{
    public class PurchaseRelationApprovalCommandHandler : IRequestHandler<PurchaseRelationApprovalCommand, Audit>
    {
        private readonly IPurchaseRelationRepository purchaseRelationRepository;
        private readonly Cross_services_Helper cross_Services_Helper;

        public PurchaseRelationApprovalCommandHandler(IPurchaseRelationRepository purchaseRelationRepository, Cross_services_Helper cross_Services_Helper)
        {
            this.purchaseRelationRepository = purchaseRelationRepository;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<Audit> Handle(PurchaseRelationApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var info = purchaseRelationRepository.All().Result.Where(x => x.PId == request.PId && x.ApprovalId == request.UserId).FirstOrDefault();
                //审批时间
                info.CreateDate = DateTime.Now;
                //审批备注
                info.ApprovalDesc = request.ApprovalDesc;
                //同意
                if (request.Identifier == 1)
                {
                    info.ApprovalState = 2;
                    await purchaseRelationRepository.Update(info);
                    var hctapprocal = new PurchaseRelation()
                    {
                        PId = request.PId,
                        ApprovalId = request.TransferId,
                        ApprovalState = 1,
                        ApprovalName = request.TransferName,
                        ApprovalDesc = null
                    };
                    var res = await purchaseRelationRepository.Create(hctapprocal);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.TransferName,
                        CreateById = request.TransferId,
                        AuditState = 1,
                        IsDel = false
                    };
                    return audit;
                }
                //拒绝
                if (request.Identifier == 2)
                {
                    info.ApprovalState = 3;
                    await purchaseRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改采购列表状态
                    var Purchase = await cross_Services_Helper.Get($"http://10.31.60.9:8000/kdrout/api/PurchaseRApi/GetPurchaseInfo?id={request.PId}");
                    var Purchases = JsonConvert.DeserializeObject<PurchaseDto>(Purchase);
                    var PurchaseInfo = new PurchaseState();
                    PurchaseInfo.id = Purchases.Id;
                    PurchaseInfo.auditState = 3;
                    Purchases.ApprovedBy = "小明";
                    var res = await cross_Services_Helper.Post($"http://10.31.60.9:8000/kkrout/api/PurchaseAPI/PurchaseAddStateShenPi", PurchaseInfo);
                    return audit;
                }
                //通过并终止转批
                if (request.Identifier == 3)
                {
                    info.ApprovalState = 2;
                    await purchaseRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改采购列表状态
                    var Purchase = await cross_Services_Helper.Get($"http://10.31.60.9:8000/kdrout/api/PurchaseRApi/GetPurchaseInfo?id={request.PId}");
                    var Purchases = JsonConvert.DeserializeObject<PurchaseDto>(Purchase);
                    var PurchaseInfo = new PurchaseState();
                    PurchaseInfo.id = Purchases.Id;
                    PurchaseInfo.auditState = 2;
                    Purchases.ApprovedBy = "小明";
                    var res = await cross_Services_Helper.Post($"http://10.31.60.9:8000/kkrout/api/PurchaseAPI/PurchaseAddStateShenPi", PurchaseInfo);

                    return audit;
                }


                return info;
            }
            catch (Exception)
            {
                return null;
                throw;
            }

            
        }
    }
}
